<?php

namespace App\Admin\Controllers;

use App\Http\Traits\GlobalConstant;
use App\Models\Teacher;
use App\Models\Users;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Log;

class TeacherController extends BaseController
{

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('教师列表')
            ->description(' ')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('教师详情')
            ->description(' ')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑教师')
            ->description(' ')
            ->body($this->form($id)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增教师')
            ->description('新增教师')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Teacher());
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1 / 2, function ($filter) {
                $filter->like('name', '教师姓名');
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('status', '状态')->select(['禁用', '正常']);

            });

        });
//
        $grid->model()->orderBy('id', 'desc');
        $grid->column('name', '姓名');
        $grid->column('avatar', '头像')->lightbox(['width' => 30, 'height' => 30]);
        $grid->column('mobile', '电话');
        $grid->column('userInfo.nickname', '关联用户');
        $grid->column('age', '年龄');
        $grid->column('sex', '性别')->display(function ($v) {
            return GlobalConstant::SEX_LIST[$v];
        });
        $grid->column('edu_max', '最高学历');
        $grid->column('status', __('状态'))->display(function ($v) {
            return GlobalConstant::_STATUS[$v];
        })->label([0 => 'danger', 1 => 'success'])->sortable();
        $grid->column('created_at', __('created_at'))->sortable();
        $grid->column('updated_at', __('updated_at'));

        $grid->actions(function ($actions) {
            $actions->disableView();
            $actions->disableDelete();
        });
        $grid->disableRowSelector();
        $grid->tools(function ($tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });
//        $grid->disableCreateButton();
        $grid->expandFilter();
        Admin::style('.img-thumbnail {width: 60px;height:60px}');
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Teacher::findOrFail($id));

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = 0)
    {
        $form = new Form(new Teacher());
        $form->text('name', '姓名')->required()->attribute(['autocomplete' => 'off']);
        $form->mobile('mobile', '手机')->required();
        $form->select('user_id', '关联用户')->required()->options(Users::where('status', 1)->pluck('nickname as text', 'id'))
            ->creationRules(['required', "unique:teacher"], ['unique' => '当前选择的用户已被关联'])
            ->updateRules(['required', "unique:teacher,user_id,{{id}}"], ['unique' => '当前选择的用户已被关联']);
        $form->image('avatar', '头像')->required()->uniqueName()->removable();
        $form->number('age', '年龄')->required()->min(1)->default(1);
//        $form->text('edu_max', '最高学历');
        $form->radio('sex', '性别')->default(1)->options(GlobalConstant::SEX_LIST);
        $form->radio('status', '状态')->options(GlobalConstant::_STATUS)->default(1);
        $form->textarea('intro', '简介');
        $form->select('approve_status', '审核状态')->options([1 => '未审核', 2 => '审核通过', 3 => '审核失败'])
            ->when(3, function (Form $form) {
                $form->textarea('approve_remark', '未通过原因');
            });
        $form->tools(function (Form\Tools $tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();
            $tools->disableView();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
            $footer->disableReset();
        });
        $form->saved(function ($form) {
            Log::info('更新用户教师信息'.$form->model()->user_id);
            if ($form->model()->approve_status == 2) {
                $user             = Users::where('id', $form->model()->user_id)->first();
                $user->teacher_id = $form->model()->id;
                $user->save();
            }
        });
        return $form;
    }

    public function update1($id)
    {
        $param                = request();
        $info                 = Teacher::find($id);
        $info->approve_status = $param['approve_status'];
        if ($param['approve_status'] == 3 && empty($param['approve_remark'])) {
            admin_error('请填写不通过原因');
            return back();
        }
        $info->approve_remark = $param['approve_remark'];
        $info->status         = $param['status'];
        $info->save();
        if ($param['approve_status'] == 2) {
            $user             = Users::where('id', $info->user_id)->first();
            $user->teacher_id = $info->id;
            $user->save();
        }
        admin_success('保存成功');
        return back();
    }
}
